import numpy
import random

class Vertex():
    
    def __init__(self, mass, position, fixed = False):
        self.mass = mass
        self.pos = position
        self.lastPos = position
        self.dampening = 0.99
        
        # randomize velocity
        randX = random.uniform(0.0, 0.2)
        randY = random.uniform(0.0, 0.2)
        randZ = random.uniform(0.0, 1.0)
        rand = numpy.array([randX, randY, randZ])
        self.vel = numpy.array([0.1, 0.1, 0.5]) - rand        
        
        self.lastVel = self.vel     
         
        self.isFixed = fixed
        
    def getAcceleration(self, dT):
        return (self.vel - self.lastVel) / dT
            
    def updatePos(self, dT):
        ''' 
        update the position according to verlet integration over time interval dT
         '''        
        if not self.isFixed:
            self.lastVel = self.vel
            self.lastPos = self.pos
            self.vel = self.lastVel+dT*self.getAcceleration(dT)
            
            # dampen
            self.vel = self.dampening * self.vel            
            self.pos = self.lastPos+ (dT * self.vel)                        
            
                    
    def applyForce(self, force, dT):        
        '''
        sum the forces applied to this object
        '''
        accel = force / self.mass
        velDelta = accel * dT
        self.vel += velDelta        
                
    def getPos(self):
        return self.pos
    
    def getLastPos(self):
        return self.lasPos
    
    def getAccel(self):
        return self.accel
    
    def getMass(self):
        return self.mass    